package test.ssl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

public class Client extends Thread
{
	private static String BASE_CODE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	private Socket socket;

	public static void main(String[] args)
		throws NoSuchAlgorithmException, IOException, KeyStoreException,
			CertificateException, UnrecoverableKeyException, KeyManagementException
	{
		//	构建证书文件流
		String path = Client.class.getResource("").getPath() + "Client.jks.pk";
		InputStream fis = new FileInputStream(path);
		String tpath = Client.class.getResource("").getPath() + "Client.jks.tk";
		InputStream tfis = new FileInputStream(tpath);

		//	构建证书存储信息对象
		KeyStore ks = KeyStore.getInstance("JKS");
		ks.load(fis, "654321".toCharArray());
		KeyStore tks = KeyStore.getInstance("JKS");
		tks.load(tfis, "654321".toCharArray());

		//	构建证书管理器工厂对象
		KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
		kmf.init(ks, "654321".toCharArray());

		//	构建受信证书管理器工厂对象
		TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
		tmf.init(tks);

		//	构建SSL上下文
		SSLContext ctx = SSLContext.getInstance("TLS");
		ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

		//	构建SSL套接字工厂并创建连接
		SSLSocketFactory sf = ctx.getSocketFactory();
		Socket socket = sf.createSocket("localhost", Server.BIND_PORT);

		//	创建交互线程
		Client client = new Client();
		client.socket = socket;
		client.start();
	}

	public void run()
	{
		//	创建显示线程
		Thread thrd = new Thread()
		{
			public void run()
			{
				//	读取信息并显示
				try
				{
					InputStream is = socket.getInputStream();
					InputStreamReader isr = new InputStreamReader(is, "UTF-8");
					BufferedReader br = new BufferedReader(isr);
					while(! socket.isClosed())
					{
						String s = br.readLine();
						if(s != null)
						{
							System.out.println(s);
						}
						Thread.sleep(100);
					}
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}
				catch(InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		};
		thrd.start();

		//	产生随机字母，并发送至服务端
		try
		{
			OutputStream os = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(os, true);
			while(! socket.isClosed())
			{
				double rand = Math.random();
				int idx1 = (int) (rand * 10000);
				idx1 %= BASE_CODE.length();
				rand = Math.random();
				int idx2 = (int) (rand * 10000);
				idx2 %= BASE_CODE.length() - idx1; 
				String s = BASE_CODE.substring(idx1, idx1 + idx2);
				pw.println(s);
				pw.flush();
				Thread.sleep(100);
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
	}
}
